;(function() {

'use strict';

// This module is only called when
// ${TiebaWhere.inside} == true.

/**
 * @type {string}
 * @const
 */
var tieUrl = location.href;

/**
 * @type {string}
 */
var fkz = '';

if ( tieUrl.match( '/p/' ) )
	fkz = tieUrl.split( '/p/' )[1].split( '?' )[0].split( '#' )[0];
else if ( tieUrl.match( 'z=' ) )
	fkz = tieUrl.split( 'z=' )[1].split( '&' )[0].split( '#' )[0];

/**
 * @type {string}
 * @const
 */
var pn = ( '' + tieUrl.match(/pn=\d+/) ).split( '=' )[1] || '1';


/**
 * @type {Object.<string,function(..*)>}
 */
var PostSuperClass = {

	/**
	 * @return {HTMLElement}
	 */
	addFloorLink : function() {
		"use strict";
		var f = $( '.d_floor, .c_floor', this ),
			pid = this.pid();
		if ( f.length === 0 || !pid ) {
			return this;
		}
		f.html( '<a href="/p/' + fkz + '?pn=' + pn + '&pid=' + pid + '#' + pid + '" style="color:#222">' + f.html() + '</a>' );
		return this;
	},

	/**
	 * @return {HTMLElement}
	 */
	addIpLink : function() {
		"use strict";
		var td = $( '.d_author_anonym', this );
		if ( td.length === 0 )
			return this;

		var ip = td.html().match( /(?:\d{1,3}\.){3}(?:\*|\d{1,3})/ );
		var ipsearchlink = '<a href="http://www.baidu.com/s?wd=' +ip+ '" target="_blank">' +ip+ '</a>';
		td.html( td.html().replace( ip, ipsearchlink ) );

		$.Ajax.get( 'http://opendata.baidu.com/api.php?query=' + ip + '&ie=utf8&format=json&tn=baidu&resource_id=6006', function(r) {
			'use strict';
			r = JSON.parse( r );
			if ( +r.status !== 0 || !r.data.length ) {
				td.html( td.html() + '<br>(幻想乡网友)' );
			} else {
				var location = r.data[0].location;
				var text = location.split(' ')[0] + '网友';
				td.html( td.html() + '<br>(' + text + ')' );
			}
		});
		return this;
	},

	/**
	 * @param {boolean=} clubFlag
	 * @return {HTMLElement}
	 */
	decode : function( clubFlag ) {
		"use strict";

		var cc = $( clubFlag ? '.post_content' : 'cc', this ),
			content = cc.children().eq( 0 ),
			pid = this.pid();
		var c = cc.text().trim();

		if ( c.substring( 0, 8 ) === '#Base64#' || c.substring( 0, 10 ) === '/#Base64#/' ) {
			c = Base64.decode( c.split(/#Base64#\/?/)[1] );
			c = c.replace( /[\n\r]+/g, '<br>' );
			content.html( '<span class=miyu-title> #Base64# </span>' + filterHTML( c ) );
			c = cc.text();
		} else if ( c.substring(0, 4) === '百度密语' && Miyu.check( c.substring(4) ) ) {
			content.html( '<span class=miyu-title> #百度密语# </span>' + filterHTML( Miyu.decode( c ) ) );
			c = cc.text();
		}

		if ( UUcode.generalDeRegExp.test( c ) ) {
			UUcode.Decode( content[0], RegExp.$1 );
		}

		return this;
	},

	/**
	 * @return {string}
	 * @nosideeffects
	 */
	pid : function() {
		return $( 'a', this ).eq( 1 ).attr('name') || '0';
	},

	/**
	 * @this {HTMLElement}
	 * @return {string} the author's ID, or IP if it is anonymous.
	 * @nosideeffects
	 */
	getAuthor : function() {
		var data = $( '.p_post', this ).attr( 'data-field' );
		var name = JSON.parse( data )['author']['name'];
		return name;
	},

	/**
	 * @return {HTMLElement}
	 */
	slash : function() {
		if ( blacklistedPersons.indexOf( this.getAuthor() ) > -1 ) {
			$( this ).addClass( 'slashed' );
			this.collapse();
		}
		return this;
	},

	/**
	 * @return {?HTMLElement}
	 */
	pre : function() {
		var ret = this.previousElementSibling;
		if ( ret && ret.className.match( 'l_post' ) )
			return ret;
		else
			return null;
	},

	/**
	 * @return {?HTMLElement}
	 */
	next : function() {
		var ret = this.nextElementSibling;
		if ( ret && ret.className.match( 'l_post' ) )
			return ret;
		else
			return null;
	},

	/**
	 * @return {HTMLElement}
	 */
	lit : function() {
		this.style.borderLeft = '1px solid #4D90F0';
		return this;
	},

	/**
	 * @return {HTMLElement}
	 */
	semiLit : function() {
		this.style.borderLeft = '1px solid #B2CCF3';
		return this;
	},

	/**
	 * @return {HTMLElement}
	 */
	focusIntoView : function() {
		"use strict";
		var height = $( this ).height(),
			windowHeight = window.innerHeight,
			pageX = $( this ).offset().top;
		var adjustment;
		if ( height < windowHeight ) {
			adjustment = ( windowHeight - height ) * 0.1;
		} else {
			adjustment = 0;
		}

		triggerScoll( pageX - adjustment );
		this.lit();

		return this;
	},

	/**
	 * @return {HTMLElement}
	 */
	collapse : function() {
		"use strict";
		$( this ).addClass( 'bdb-collapse' );

		var mainPart = $( 'table', this ).eq(0);
		if ( mainPart.length === 0 )
			return this;
		mainPart.addClass( 'hid' );

		if ( $( '.floor_cover', this ).length ) {
			$( '.floor_cover', this ).removeClass( 'hid' );
			return this;
		}

		var PostInfo = JSON.parse( $( '.p_post', this )[0].dataset.field );
		var name = PostInfo.author.name, floorNum = PostInfo.content.floor;

		var coverContent = '<span class="d_name">' + '作者：' + name + '</span>' +
				'<span class=d_floor style=float:right>' + floorNum + '楼' + '</span>';
		var cover = $( '<table>' ).attr( 'class', 'floor_cover' ).html( coverContent );

		if ( $( '.d_post_split', this ).length ) {
			mainPart.parent()[0].insertBefore( cover[0], mainPart.siblings().get( -1 ) );
		} else { // For classic version
			mainPart.append( cover );
		}

		return this;

	},

	/**
	 * @return {HTMLElement}
	 */
	expand : function() {
		"use strict";
		if ( $(this).hasClass('bdb-collapse') ) {
			$(this).removeClass('bdb-collapse');

			$('.floor_cover', this).addClass('hid');

			var mainPart = $( 'table', this ).eq(0);
			mainPart.removeClass( 'hid' );
		}

		this.focusIntoView();
		return this;

	},

	/**
	 * @return {HTMLElement}
	 */
	toggle: function() {
		if ( $( this ).hasClass( 'bdb-collapse' ) ) {
			return this.expand();
		} else {
			return this.collapse();
		}
	}

}; // end of ${PostSuperClass}

/**
 * Smooth scroll to (0, y).
 * @param {number} y
 */
function triggerScoll( y ) {
	"use strict";

	triggerScoll.destination = y;

	var inter = function() {
		"use strict";
		var _beforeStep = document.body.scrollTop;
		scrollBy( 0, ( y - _beforeStep ) * 0.33 >>> 0 );
		if ( document.body.scrollTop !== _beforeStep && triggerScoll.destination === y )
			requestAnimationFrame( inter );
	};

	requestAnimationFrame( inter );
}

/**
 * Anti XSS for miyu.
 * @param {string} text
 * @return {string}
 * @nosideeffects
 */
function filterHTML( text ) {
	return text.replace( /<(?:[^i]|\s*?if).*?>/g, '' ).replace( /<img[^>]+?on\w{3,}[^>]*?>/ig, '' );
}


// Expose to global.
window.PostSuperClass = PostSuperClass;


})();
